package sh.base.aop.util;

import sh.base.aop.ReflectException;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

/**
 * @author lqq
 * @date 2019/1/22
 */
public abstract class ReflectUtil {


    public static void setFieldValue(Field field , Object target, Object value) {
        try {
            field.setAccessible(true);
            field.set(target,value);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
            throw new ReflectException("设置属性值异常" + e.getMessage());
        } catch (Exception e){
            e.printStackTrace();
            throw new ReflectException("未知异常" + e.getMessage());
        }
    }


    /**
     * 获取List中真正的泛型类型
     * @param parameterizedType 参数化的类型
     * @return
     */
    public static Class<?> getListGenericClass(Type parameterizedType){
        if(parameterizedType instanceof ParameterizedType){
            ParameterizedType pt = (ParameterizedType) parameterizedType;
            return  (Class<?>)pt.getActualTypeArguments()[0];
        }
        throw new ReflectException("暂不支持获取泛型类型" + parameterizedType);
    }

    /**
     *  获取对象中含有指定注解的属性的值
     * @param object 获取值的对象
     * @param clazz 对象的class，因为是递归调用，所以需要传入
     * @param annotationClass 属性的注解
     * @return
     */
    public static <T> T getFieldValueWithAnnotation(Object object, Class<?> clazz, Class<? extends Annotation> annotationClass){
        Field[] fields = clazz.getDeclaredFields();
        for(Field field : fields){
            if(field.isAnnotationPresent(annotationClass)){
                field.setAccessible(true);
                try {
                    return (T)field.get(object);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                    throw new ReflectException("获取id异常");
                }
            }
        }
        if("Object".equals(clazz.getSuperclass().getSimpleName()) ){
            // 没有找到id
            return null;
        }
        return ReflectUtil.getFieldValueWithAnnotation(object, clazz.getSuperclass(), annotationClass);
    }


    /**
     * 判断指定的类中是否含有特定的注解
     * @param clazz
     * @param annotationClass
     * @return
     */
    public static boolean methodHasAnnotation(Class<?> clazz, Class<? extends Annotation> annotationClass) {
        //TODO
        return true;
    }

    /**
     * 获取有对应注解的属性list
     * @param targetClass 要查找的类
     * @param annotationClass 对应的注解
     * @return
     */
    public static List<Field> getAutowiredFields(Class<?> targetClass, Class<? extends Annotation> annotationClass) {
        List<Field> fieldList = new ArrayList<>();
        fillListThatHasAnnotation(targetClass, annotationClass, fieldList);
        return fieldList;
    }

    /**
     * 找到对应注解的field
     * @param targetClass
     * @param annotationClass
     * @param fieldList 放field的桶
     */
    private static void fillListThatHasAnnotation(Class<?> targetClass, Class<? extends Annotation> annotationClass, List<Field> fieldList){
        Field[] fields = targetClass.getDeclaredFields();
        for(Field field : fields){
            field.setAccessible(true);
            if(field.isAnnotationPresent(annotationClass)){
               fieldList.add(field);
            }
        }
        if(!"Object".equals(targetClass.getSuperclass().getSimpleName())){
            fillListThatHasAnnotation(targetClass.getSuperclass(), annotationClass, fieldList);
        }
    }

    /**
     * 通过反射获取对象
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> Object getInstance(Class<T> clazz) {
        try {
            return clazz.newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
            throw new ReflectException("反射出现了异常" + e.getMessage());
        } catch (IllegalAccessException e) {
            e.printStackTrace();
            throw new ReflectException("反射出现了异常" + e.getMessage());
        }

    }

    /**
     * 根据类的名字加载类
     * @param className
     * @return
     */
    public static Object getInstance(String className) {
        try {
            return Class.forName(className).newInstance();
        } catch (Exception e) {
            e.printStackTrace();
            throw new ReflectException("获取类的对象时异常" + e.getMessage());
        }
    }
}
